package com.uzai.console.service.smscode.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.uzai.common.enums.MerConfigKeyEnum;
import com.uzai.common.enums.MerConfigTypeEnum;
import com.uzai.common.exception.BusinessException;
import com.uzai.common.result.ResponseCode;
import com.uzai.console.common.ResponseMsg;
import com.uzai.console.common.utils.DateUtil;
import com.uzai.console.common.utils.Tools;
import com.uzai.console.config.UzaiConsoleSysConfig;
import com.uzai.console.dto.smscode.SmsCodeDto;
import com.uzai.console.entity.MerBaseConfig;
import com.uzai.console.entity.Merchant;
import com.uzai.console.enums.SmsType;
import com.uzai.console.mapper.MerBaseConfigMapper;
import com.uzai.console.service.merchant.MerchantService;
import com.uzai.console.service.smscode.SmsCodeService;
import com.uzai.console.vo.smscode.SmsCodeVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * @author liuqi
 * @date 2021年08月23日 20:25
 */
@Service
public class SmsCodeServiceImpl implements SmsCodeService {

    @Autowired
    private MerBaseConfigMapper merBaseConfigMapper;
    @Autowired
    private UzaiConsoleSysConfig uzaiConsoleSysConfig;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private MerchantService merchantService;

    /**
     * 查询短信验证功能详情
     *
     * @return null
     * @author liuqi
     * @date 2021/8/13 11:38
     */
    public SmsCodeVo findSmsCode(Long merId) {

        SmsCodeVo smsCodeVo = new SmsCodeVo();

        //查询运营商短信验证功能信息
        MerBaseConfig merBaseConfig = merBaseConfigMapper.selectByMerId(merId, MerConfigKeyEnum.SMS_CODE_CONFIG.getValue());
        if (merBaseConfig != null) {
            smsCodeVo = JSONObject.parseObject(merBaseConfig.getVal(), SmsCodeVo.class);
        }
        return smsCodeVo;
    }

    /**
     * 修改短信验证功能详情
     *
     * @param smsCodeDto
     * @return null
     * @author liuqi
     * @date 2021/8/13 11:38
     */
    public void updateSmsCode(SmsCodeDto smsCodeDto) {

        Long merId = smsCodeDto.getMerId();
        Merchant merchant = merchantService.findByMerId(merId);
        if(merchant == null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "运营商不存在");
        }

        if(!uzaiConsoleSysConfig.getDefaultPassword().equals(smsCodeDto.getCode())){
            //获取存在redis中的验证码
            String codeByRedis = (String) redisTemplate.opsForValue().get(String.format(SmsType.CONFIG_SMS_CODE.getDesc(), merchant.getMobile()));
            //判断验证码是否过期
            if (StringUtils.isBlank(codeByRedis)){
                throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.CODE_MISS);
            }
            //验证码是否正确
            if (!codeByRedis.equals(smsCodeDto.getCode()) && !uzaiConsoleSysConfig.getDefaultPassword().equals(smsCodeDto.getCode())){
                throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.CODE_ERR);
            }
            //验证成功，删除验证码
            redisTemplate.delete(String.format(SmsType.CONFIG_SMS_CODE.getDesc(), merchant.getMobile()));
        }

        SmsCodeVo smsCodeVo = new SmsCodeVo();
        BeanUtils.copyProperties(smsCodeDto, smsCodeVo);

        MerBaseConfig merBaseConfig = merBaseConfigMapper.selectByMerId(smsCodeDto.getMerId(), MerConfigKeyEnum.SMS_CODE_CONFIG.getValue());

        Integer now = DateUtil.getNowTime();
        //修改
        if (merBaseConfig != null) {
            merBaseConfig.setVal(Tools.getStrEmpty(JSONObject.toJSONString(smsCodeVo)));
            merBaseConfig.setUpdateTime(now);
            merBaseConfigMapper.update(merBaseConfig);
        } else { //增加
            merBaseConfig = new MerBaseConfig();
            merBaseConfig.setId(IdWorker.getId());
            merBaseConfig.setMerId(smsCodeDto.getMerId());
            merBaseConfig.setType(MerConfigTypeEnum.PRODUCTS.getValue());
            merBaseConfig.setKey(MerConfigKeyEnum.SMS_CODE_CONFIG.getValue());
            merBaseConfig.setVal(Tools.getStrEmpty(JSONObject.toJSONString(smsCodeVo)));
            merBaseConfig.setCreateTime(now);
            merBaseConfig.setUpdateTime(now);
            merBaseConfigMapper.insert(merBaseConfig);
        }
    }

}
